<?php

class FMSException extends Exception {
	private $ErrorType;

	function __construct($message, $err_type = "ND"){
		parent::__construct($message);
		$this->ErrorType = $this->getErrorType($err_type);
		$this->writeToLog();
	}
	
	function getErrorType($err_type){
		$ErrorsPathFile = dirname(__FILE__)."/../settings/errors.xml";
		$Errors = @simplexml_load_file($ErrorsPathFile); 
		
		if(!$Errors){
			die("Cannot load < errors.xml > file.");
		}
		
		$Find = $Errors->xpath("//error[@code=\"$err_type\"]");
		
		return (string)$Find[0];
	}
	
	function writeToLog(){
		$LogPath = dirname(__FILE__)."/../smdir/error_log.txt";
		
		$NewEntry = "--------------------------------\n";
		$NewEntry .= "Error: File <".parent::getFile().">, line ".parent::getLine()."\n";
		$NewEntry .= "Message: ".parent::getMessage()." [".$this->ErrorType."]\n";
		$NewEntry .= "User: ".$_SERVER['REMOTE_ADDR']." at ".date("d/m/Y H:i:s")."\n";
		
		if(!file_put_contents($LogPath, $NewEntry, FILE_APPEND)){
			die("Cannot write to log file.");
		}
		
		return true;
	}
	
	function display(){
		$Display = "<div style=\"border: 1px dashed #c0c0c0; padding: 10px; background: #EFEFEF;\">";
			$Display .= "<h3>".$this->ErrorType."</h3>";
			$Display .= "<strong>Message: </strong>".parent::getMessage()."<br />";
			$Display .= "<strong>File: </strong>".parent::getFile()."<br />";
			$Display .= "<strong>Line: </strong>".parent::getLine()."<br />";
		$Display .= "</div>";
		
		return $Display;
	}
}

?>